add current directory to sys.path for Windows module resolution#299
add current directory to sys.path for Windows module resolution#299MohammadRaziei wants to merge 1 commit intopybind:mainfrom
Conversation
Insert the current working directory at the beginning of sys.path before processing modules. This ensures that local modules can be resolved correctly when pybind11-stubgen is run, preventing import errors for modules located in the current directory.
|
Why do you think this is not a good idea?? I believe that this is a necessary option, and I was shocked when I found that this was missed. |
|
Sorry, such inserts have a lot of unwanted side effects when used in larger or integrated projects. We will close this, because this would introduce an anti-pattern: If any code change were acceptable, it would need to be explicit and opt-in, not a hidden global For example, in CMake: add_custom_target(stubs
COMMAND ${CMAKE_COMMAND} -E env
"PYTHONPATH=$<TARGET_FILE_DIR:mymodule>"
${Python_EXECUTABLE} -m pybind11_stubgen mymodule
-o ${CMAKE_CURRENT_BINARY_DIR}/stubs
DEPENDS mymodule
)The same pattern works for |
On Windows, when running pybind11-stubgen from a build directory
(e.g., via CMake), the compiled module (.pyd file) cannot be found
because it's not in Python's search path.
This fix adds the current working directory to sys.path before
importing the module, ensuring compatibility with Windows builds
where the module exists in the build directory.
Example on Windows:
build/src/mymodule.cp311-win_amd64.pyd ← exists but not found
Without this fix:
ModuleNotFoundError: No module named 'mymodule'
With this fix:
Module imports successfully ✓